package com.abc.struts.test;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;
import org.springframework.stereotype.Component;

import struts.ext.annotation.Action;
import struts.ext.annotation.Forward;
import struts.ext.annotation.Return;
import struts.ext.annotation.ReturnType;
import struts.ext.base.ExtDispatchAction;
import struts.ext.base.ExtServletWrapper;
import struts.ext.commons.lang.collection.Maps;
import struts.ext.util.Bean2ExcelRowConfig;
import struts.ext.util.BeanList2ExcelConfig;
import struts.ext.util.ExcelReturnWrapper;
import struts.ext.util.Jsons;
import struts.ext.util.converter.BooleanFormatter;
import struts.ext.util.converter.DateFormatter;


@Component //纳入spring管理。如果@Action中path为/aBC,类型为ABC的话，可以省略value。
           //ExtRequestProcessor的getDelegateAction方法实现了这个功能
@Action(path="/myAction",
		parameter="method", //默认也是method
		scope="request", //默认是request
		validate=false, //默认是false
		//form = TestForm.class,//直接指定form的类 或者像下面这样指定在配置文件中配置的form的name
		name="myForm",
		forwards={ //或者使用@Forwards也可以
			@Forward(name="test_result",path="/WEB-INF/page/front/test/test1.jsp"),
			@Forward(name="login",path="/login.jsp")
		})
/**
 * 测试方法:
 * 在浏览器中输入:http://127.0.0.1:8080/Test_Emp_Help_LR//myAction.do?method=tologin
 * 输入用户名和密码提交。
 * 正常应在控制台打印出用户名和密码，并且前台显示success。
 */
public class MyAction extends ExtDispatchAction {
	
	@Return(value=ReturnType.Excel)
	public Object exportExcel(ExtServletWrapper wrapper){
		List<TestForm> forms = new ArrayList<TestForm>();
		for(int i=0; i < 100; i++){
			forms.add(new TestForm("user"+i, "pass"+i, 
						Math.random()>0.5? true:false, new Date(), Math.random()*10000,
						new Role(new Random().nextInt(1000000),"name"+i)));
		}
		Object[] attr_values = new Object[]{ //标题名3个字对应的长度在15左右
				"username",	new Bean2ExcelRowConfig("用户名", 15, null),
				"password",	new Bean2ExcelRowConfig("密码", 12, null),
				"married", 	new Bean2ExcelRowConfig("婚否",12,new BooleanFormatter("已婚", "未婚")),
				"birth", 	new Bean2ExcelRowConfig("出生年月",25,new DateFormatter()),
				"weight",	new Bean2ExcelRowConfig("体重",30,null),
				"role.name",new Bean2ExcelRowConfig("角色名",15,null),
				"role.id",new Bean2ExcelRowConfig("角色Id",15,null)
		};
		return new BeanList2ExcelConfig("用户表", "工作区", forms, Maps.orderMap(String.class, Bean2ExcelRowConfig.class, attr_values));
	}
	
	public String tologin(ExtServletWrapper wrapper){
		return "login";
	}
	
	public String login(ExtServletWrapper wrapper) throws Exception{
		TestForm form = (TestForm)wrapper.getForm();
		System.out.println(form.getUsername());
		System.out.println(form.getPassword());
		
		wrapper.getResponse().getWriter().write("Success！");
		return null;
	}
	
	@Return(ReturnType.Json)
	public TestForm test(ExtServletWrapper wrapper){
		return new TestForm("xueshi", "shasha");
	}
	
	@Return(ReturnType.Json)
	public Object testJson(ExtServletWrapper wrapper){
		List<Object> list = new ArrayList<Object>();
		list.add(new TestForm("",""));
		return list;
	}
	
	@Return(ReturnType.DownLoad)
	public Object testDownload(ExtServletWrapper param){
		return new File("K:\\Test.java");
	}
	
//	@Return(ReturnType.Json)
	public Object testJson2(ExtServletWrapper wrapper){
		List<Object> list = new ArrayList<Object>();
		list.add(new TestForm("",""));
		return list;
	}
	
	public ActionForward testJson3(ExtServletWrapper wrapper) throws Exception{
		List<Object> list = new ArrayList<Object>();
		list.add(new TestForm("",""));
		
		HttpServletResponse response = wrapper.getResponse();
		response.setContentType("text/json;charset=UTF-8");
    	response.getWriter().write(Jsons.toJson(list));
    	return null;
	}
	
	public String defaultMethod(ExtServletWrapper wrapper){
		return "login";
	}
}
